package com.lanting.meetingserver.test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lanting.meetingserver.dao.UserDao;
import com.lanting.meetingserver.domain.User;
import com.lanting.meetingserver.query.UserQuery;
import com.lanting.meetingserver.util.CommonEnum;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class UserDaoCase {

	
	@Resource
	private UserDao userDao;



	@Test
	public void TestUserDao_Insert(){
		User user;
		int lastUserId=0;
		//添加一个用户
		for (int i = 0; i <4; i++) {
			user = new User();
			user.setUserName(String.format("张勇%d",i));
			user.setUserEmail("michael_lanting@126.com");
			user.setUserPassword("123");
			user.setUserPosition("程序员"+i);
			user.setModifier(1);
			user.setLoginId("yong.zhang"+i);
			//user.setModifyDate(new Date());
			userDao.insert(user);
			lastUserId = user.getUserId();
		}
		
		System.out.println("添加用户成功");
		System.out.println(String.format("新添加的UserID为%d",lastUserId));
		
		//修改一个用户
		user = userDao.getUserById(lastUserId);
		
		if (user != null) {
			System.out.println("修改前的数据："+user.toString());
			user.setUserEmail("zy780529@gmail.com");
			user.setUserName("王志国");
			userDao.update(user);
			user = userDao.getUserById(user.getUserId());
			System.out.println(user.toString());
		}else {
			System.out.println("更新数据失败，原因：没有获取到user");
		}
		//获取全部列表
		List<User> list =userDao.getAll();
		for (User user2 : list) {
			System.out.println(user2.toString());
		}

		//删除一个用户
		user = userDao.getUserById(user.getUserId());
		if (user != null) {
			userDao.delete(user);
			System.out.println(String.format("删除%d号user成功！",user.getUserId()));
		}
		else {
			System.out.println("删除数据失败，原因：没有获取到user");
		}

		//制造一个查询条件进行查询，并且俺姓名进行排序
		Map<String, Integer> orderbyMap = new HashMap<String, Integer>();
		orderbyMap.put("userName",CommonEnum.ORDERBY_DESC);
		String queryParam = " modifier > 0 ";
		UserQuery query = new UserQuery(1, 10, orderbyMap, queryParam);
		List<User> list2 = userDao.getList(query);
		for (User user2 : list2) {
			System.out.println(user2.toString());
		}
	}
	
	@Test
	public void TestUserLogin(){
		User user = userDao.getUserByLoginIdAndPassord("yong.zhang","123");
		if (user!=null) {
			System.out.println("登录成功");
		}else {
			System.out.println("登录失败");
		}
		
	}
	
	@Test
	public void testQueryUser() {
		UserQuery query = new UserQuery();
		query.setLoginId("system");
		query.setUserName("SYSTEM");
System.out.println(query.getQueryString());		
		System.out.println(userDao.getCountByQuery(query));
		System.out.println(userDao.getList(query)); 
	}
	
}
